//
//  ClinicSearchBar.m
//  ybzj
//
//  Created by liyuan on 15/7/6.
//  Copyright (c) 2015年 liyuan. All rights reserved.
//

#import "ClinicSearchBar.h"

@implementation ClinicSearchBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)awakeFromNib{
    UITextField *searchField;
    self.tintColor=[UIColor blackColor];
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0){
        searchField=[self.subviews objectAtIndex:1];
    }
    else{
        //remove default background view and set textfield view
        
        searchField=[((UIView *)[self.subviews lastObject]).subviews lastObject];
        searchField.translatesAutoresizingMaskIntoConstraints=NO;
        
        //set constrains for search textfield
        NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:searchField
                                                                          attribute:NSLayoutAttributeLeading
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:self
                                                                          attribute:NSLayoutAttributeLeading
                                                                         multiplier:1.0f
                                                                           constant:width*16/640];
        
        NSLayoutConstraint* rightConstraint = [NSLayoutConstraint constraintWithItem:searchField
                                                                           attribute:NSLayoutAttributeTrailing
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self
                                                                           attribute:NSLayoutAttributeTrailing
                                                                          multiplier:1.0f
                                                                            constant:0-width*16/640];
        
        NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:searchField
                                                                         attribute:NSLayoutAttributeTop
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:self
                                                                         attribute:NSLayoutAttributeTop
                                                                        multiplier:1.0f
                                                                          constant:self
.bounds.size.width*16/640];
        
        NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:searchField
                                                                            attribute:NSLayoutAttributeBottom
                                                                            relatedBy:NSLayoutRelationEqual
                                                                               toItem:self
                                                                            attribute:NSLayoutAttributeBottom
                                                                           multiplier:1.0f
                                                                             constant:0-self.bounds.size.width*16/640];
        
        leftConstraint.active = YES;
        rightConstraint.active = YES;
        topConstraint.active = YES;
        heightConstraint.active = YES;
        //set colors for textfield
        searchField.layer.cornerRadius=8.0f;
        searchField.layer.masksToBounds=YES;
        searchField.layer.borderColor=[[UIColor colorWithRed:195/255.0 green:195/255.0 blue:196/255.0 alpha:1]CGColor];
        searchField.layer.borderWidth= 1.2f;
        self.backgroundColor=[UIColor colorWithRed:246/255.0 green:246/255.0 blue:247/255.0 alpha:1];
        [((UIView *)[((UIView *)[self.subviews objectAtIndex:0]).subviews objectAtIndex:0]) removeFromSuperview];
        for(UIView * view in ((UIView *)[searchField.subviews objectAtIndex:0]).subviews){
            [view removeFromSuperview];
        }
        
        ((UIView *)[searchField.subviews objectAtIndex:0]).backgroundColor=[UIColor whiteColor];
        
    }
}

@end
